package com.bci.internal.engine.bcibpmengine.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bci.internal.engine.bcibpmengine.instances.entity.ActReProcdef;

import com.bci.internal.engine.bcibpmengine.instances.mapper.ActReProcdefMapper;

import com.bci.internal.engine.bcibpmengine.service.ActivitiProcessInstanceService;
import com.bci.internal.engine.bcibpmengine.vo.ProcessInstanceVo;

import com.fasterxml.jackson.databind.util.ObjectBuffer;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;


import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;


import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * @author jzwu
 * @since 2021/4/5 0005
 */
@Slf4j
@Service
public class ActivitiProcessInstanceImpl implements ActivitiProcessInstanceService {
    @Resource
    private RuntimeService runtimeService;

    @Resource
    private RepositoryService repositoryService;

    @Resource
    private ActReProcdefMapper actReProcdefMapper;


    @Override
    public ProcessInstanceVo initProcessInstance(String processDeploymentId, String instanceName, String instanceVariable) {
        LambdaQueryWrapper<ActReProcdef> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ActReProcdef::getDeploymentId, processDeploymentId).select(ActReProcdef::getId);
        ActReProcdef actReProcdef = actReProcdefMapper.selectOne(lambdaQueryWrapper);
        Assert.notNull(actReProcdef, "该部署文件下没有对应流程实例文件！");
        ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
                .processDefinitionId(actReProcdef.getId())
                .variable("instanceVariable", instanceVariable)
                .name(instanceName).start();
        ProcessInstanceVo processInstanceVo = new ProcessInstanceVo();
        BeanUtils.copyProperties(processInstance, processInstanceVo);
        return processInstanceVo;
    }


    @Override
    public List<HashMap<String, Object>> listProcessDefinitions(int page, int pageSize) {
        List<ProcessDefinition> listDeployments = repositoryService.createProcessDefinitionQuery().listPage(page, pageSize);
        List<HashMap<String, Object>> listMap = new ArrayList<HashMap<String, Object>>();

        listDeployments.sort((y, x) -> x.getVersion() - y.getVersion());

        for (ProcessDefinition pd : listDeployments) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("processDefinitionID", pd.getId());
            hashMap.put("name", pd.getName());
            hashMap.put("key", pd.getKey());
            hashMap.put("resourceName", pd.getResourceName());
            hashMap.put("deploymentID", pd.getDeploymentId());
            hashMap.put("version", pd.getVersion());
            listMap.add(hashMap);
        }
        return listMap;
    }
}